package com.isoft.c2team3service2.controller;

import com.isoft.c2team3service2.bean.PageResult;
import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.entity.UserMessageVO;
import com.isoft.c2team3service2.service.UserMessageService;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private UserMessageService userMessageService;

    // 新增：发送消息接口
    @PostMapping("/send")
    public ResponseData<String> sendMessage(
            @RequestParam Integer receiverId,  // 接收者ID
            @RequestParam String content) {    // 消息内容

        // 从ThreadLocal获取当前登录用户（发送者）
        UserDTO currentUser = UserHolder.getUser();
        if (currentUser == null) {
            return new ResponseData<>(401, "用户未登录", null);
        }

        // 调用Service层发送消息（含业务校验）
        boolean isSuccess = userMessageService.sendMessage(receiverId, content);
        if (isSuccess) {
            return new ResponseData<>(200, "消息发送成功", null);
        } else {
            return new ResponseData<>(400, "消息发送失败（可能未关注或已有未回复消息）", null);
        }
    }

    // 获取历史对话
    @GetMapping("/conversation")
    public ResponseData<List<UserMessageVO>> getConversation(
            @RequestParam Integer otherUserId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        UserDTO currentUser = UserHolder.getUser();
        if (currentUser == null) {
            return new ResponseData<>(401, "用户未登录", null);
        }
        List<UserMessageVO> messages = userMessageService.getConversation(otherUserId, page, size);
        return new ResponseData<>(200, "获取会话成功", messages);
    }
    // 原有接口：获取历史会话、批量标记已读（保持不变）
    @PostMapping("/read")
    public ResponseData<Integer> markAsRead(
            @RequestBody List<Integer> messageIds) {
        UserDTO currentUser = UserHolder.getUser();
        if (currentUser == null) {
            return new ResponseData<>(401, "用户未登录", 0);
        }
        boolean success = userMessageService.markAsRead(messageIds);
        int count = success ? messageIds.size() : 0;
        return new ResponseData<>(200, "标记已读成功", count);
    }
    /**
     * 新增接口：查询别人给自己发的消息（分页+可选状态筛选）
     * 请求路径：GET /api/messages/received
     * 示例：/api/messages/received?page=1&size=10&status=0（查第1页未读消息，页大小10）
     * @param page 页码（默认1）
     * @param size 页大小（默认20）
     * @param status 消息状态（可选：0-未读，1-已读，不传查所有）
     * @return 分页消息结果
     */
    @GetMapping("/received")
    public ResponseData<PageResult<UserMessageVO>> getReceivedMessages(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Integer status) { // required=false：可选参数

        // 1. 登录校验（未登录返回401）
        UserDTO currentUser = UserHolder.getUser();
        if (currentUser == null) {
            return new ResponseData<>(401, "用户未登录", null);
        }

        // 2. 调用Service获取分页消息
        PageResult<UserMessageVO> pageResult = userMessageService.getReceivedMessages(page, size, status);

        // 3. 返回成功结果
        return new ResponseData<>(200, "查询收到的消息成功", pageResult);
    }
}